package DataProcess;

import baseAPI.AlertStage;
import baseAPI.Animation;
import baseAPI.Database;
import baseClass.Admin;
import baseClass.Member;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class VipInfoPage {
    private static Database database;
    private static Label labelName, labelId, labelPhone, labelLevel, labelPoint;
    private static TextField idText;
    private static Button btnSearch;
    private static Button btnSign;

    public static void bind(Parent vipInfo, Database database) {
        VipInfoPage.database = database;//数据库初始化

        //控件绑定
        labelName = (Label) vipInfo.lookup("#labelName");
        labelId = (Label) vipInfo.lookup("#labelId");
        labelPhone = (Label) vipInfo.lookup("#labelPhone");
        labelLevel = (Label) vipInfo.lookup("#labelLevel");
        labelPoint = (Label) vipInfo.lookup("#labelPoint");
        idText = (TextField) vipInfo.lookup("#idText");
        btnSearch = (Button) vipInfo.lookup("#btnSearch");
        btnSign = (Button) vipInfo.lookup("#btnSign");

        btnSearch.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                search();
            }
        });

        idText.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode() == KeyCode.ENTER)
                {
                    search();
                }
            }
        });

        btnSign.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                sign();
            }
        });
    }

    private static void search() {
        String idNumber = idText.getText();
        if (idNumber.equals("")) {
            idText.requestFocus();
            AlertStage.show("提示", "请输入身份证号！");
            return;
        }

        Member member = Member.getThroughIdNumber(idNumber, database);

        if (member == null) {
            idText.requestFocus();
            AlertStage.show("提示", "未查询到会员信息，请重试！");
            return;
        }
        String[] information = member.getMemberInformation();
        labelId.setText(information[0]);
        labelPoint.setText(information[2]);
        labelLevel.setText(information[3]);

        //隐私保护
        StringBuilder phoneNumber = new StringBuilder(information[4]);
        labelPhone.setText(phoneNumber.replace(3, 7, "****").toString());
        labelName.setText(information[5]);
    }

    public static void sign() {
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.getIcons().add(new Image("/pic/ico.png"));
        stage.setTitle("会员注册");
        stage.setResizable(false);
        stage.setWidth(400);
        stage.setHeight(595);

        TextField userIdField = new TextField();
        TextField nameField = new TextField();
        TextField phoneField = new TextField();
        Button check = new Button();

        DropShadow dropShadow = new DropShadow();
        dropShadow.setOffsetY(1.0);
        dropShadow.setOffsetX(1.0);
        dropShadow.setColor(Color.rgb(129, 192, 212));
        dropShadow.setSpread(0.2);

        userIdField.setLayoutX(80);
        userIdField.setLayoutY(145);
        userIdField.setPrefWidth(280);
        userIdField.setPrefHeight(50);
        userIdField.setPromptText("身份证号");
        userIdField.setStyle(
                "    -fx-text-fill:white;\n" +
                        "    -fx-opacity: 0.7;\n" +
                        "    -fx-font-size: 16pt;\n" +
                        "    -fx-font-family: \"MV Boli\";\n" +
                        "    -fx-font-weight: bold;\n" +
                        "    -fx-border-radius: 15px;\n" +
                        "    -fx-background-radius: 15px;\n" +
                        "    -fx-background-color: rgb(20,23,34);\n" +
                        "    -fx-alignment: center");
        userIdField.setEffect(dropShadow);

        nameField.setLayoutX(80);
        nameField.setLayoutY(220);
        nameField.setPrefWidth(280);
        nameField.setPrefHeight(50);
        nameField.setPromptText("姓名");
        nameField.setStyle(
                "    -fx-text-fill:white;\n" +
                        "    -fx-font-size: 16pt;\n" +
                        "    -fx-opacity: 0.7;\n" +
                        "    -fx-font-family: \"MV Boli\";\n" +
                        "    -fx-font-weight: bold;\n" +
                        "    -fx-border-radius: 15px;\n" +
                        "    -fx-background-radius: 15px;\n" +
                        "    -fx-background-color: rgb(20,23,34);\n" +
                        "    -fx-alignment: center");
        nameField.setEffect(dropShadow);

        phoneField.setLayoutX(80);
        phoneField.setLayoutY(295);
        phoneField.setPrefWidth(280);
        phoneField.setPrefHeight(50);
        phoneField.setPromptText("预留手机号");
        phoneField.setStyle(
                "    -fx-text-fill:white;\n" +
                        "    -fx-font-size: 16pt;\n" +
                        "    -fx-opacity: 0.7;\n" +
                        "    -fx-font-family: \"MV Boli\";\n" +
                        "    -fx-font-weight: bold;\n" +
                        "    -fx-border-radius: 15px;\n" +
                        "    -fx-background-radius: 15px;\n" +
                        "    -fx-background-color: rgb(20,23,34);\n" +
                        "    -fx-alignment: center");
        phoneField.setEffect(dropShadow);

        check.setLayoutX(170);
        check.setLayoutY(360);
        check.setPrefWidth(60);
        check.setPrefHeight(60);
        check.setStyle("-fx-background-image:url(/pic/check.png);-fx-background-color: transparent;");

        check.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Animation.checkButton(check);
            }
        });
        check.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Animation.resetCheck(check);
            }
        });
        check.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Animation.checked(check);
            }
        });
        check.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Animation.reset1(check);
            }
        });
        check.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (idNumRight(userIdField.getText())&&Member.getThroughIdNumber(userIdField.getText(),database)==null) {
                    if (nameField.getText().equals("") == false) {
                        if (phoneField.getText().matches("[\\d]{11}")&&Member.getThroughPhoneNumber(phoneField.getText(),database)==null) {
                            String s = "123456";
                            while (Member.getThroughId(s, database) != null) {
                                s = random();
                            }
                            Member member = new Member(s, userIdField.getText(), 0, 1, phoneField.getText(), nameField.getText());
                            member.addMember(database);
                            AlertStage.show("提示","会员注册成功！");
                            stage.close();
                        }
                        else if(Member.getThroughPhoneNumber(phoneField.getText(),database)!=null) {
                            phoneField.requestFocus();
                            AlertStage.show("提示","该手机号已经绑定，请勿重复绑定");
                        }
                        else{
                                phoneField.requestFocus();
                                AlertStage.show("提示", "请输入正确的手机号");
                            }
                        }
                    else {
                        nameField.requestFocus();
                        AlertStage.show("提示", "请输入姓名");
                    }
                } else if(Member.getThroughIdNumber(userIdField.getText(),database)!=null){
                    userIdField.requestFocus();
                    AlertStage.show("提示","用户已存在，请勿重复注册");
                }
                else{
                    userIdField.requestFocus();
                    AlertStage.show("提示", "请输入正确的身份证号");
                }
            }
        });

        userIdField.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode() == KeyCode.ENTER)
                {
                    nameField.requestFocus();
                }
            }
        });

        nameField.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode() == KeyCode.ENTER)
                {
                    phoneField.requestFocus();
                }
            }
        });

        phoneField.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode() == KeyCode.ENTER)
                {
                    Animation.checked(check);
                    if (idNumRight(userIdField.getText())&&Member.getThroughIdNumber(userIdField.getText(),database)==null) {
                        if (nameField.getText().equals("") == false) {
                            if (phoneField.getText().matches("[\\d]{11}")&&Member.getThroughPhoneNumber(phoneField.getText(),database)==null) {
                                String s = "123456";
                                while (Member.getThroughId(s, database) != null) {
                                    s = random();
                                }
                                Member member = new Member(s, userIdField.getText(), 0, 1, phoneField.getText(), nameField.getText());
                                member.addMember(database);
                                Animation.reset1(check);
                                AlertStage.show("提示","会员注册成功！");
                                stage.close();
                            }
                            else if(Member.getThroughPhoneNumber(phoneField.getText(),database)!=null) {
                                Animation.reset1(check);
                                phoneField.requestFocus();
                                AlertStage.show("提示","该手机号已经绑定，请勿重复绑定");
                            }
                            else{
                                Animation.reset1(check);
                                phoneField.requestFocus();
                                AlertStage.show("提示", "请输入正确的手机号");
                            }
                        }
                        else {
                            Animation.reset1(check);
                            nameField.requestFocus();
                            AlertStage.show("提示", "请输入姓名");
                        }
                    } else if(Member.getThroughIdNumber(userIdField.getText(),database)!=null){
                        Animation.reset1(check);
                        userIdField.requestFocus();
                        AlertStage.show("提示","用户已存在，请勿重复注册");
                    }
                    else{
                        Animation.reset1(check);
                        userIdField.requestFocus();
                        AlertStage.show("提示", "请输入正确的身份证号");
                    }
                }
            }
        });

        Pane pane = new Pane(userIdField, nameField, phoneField, check);
        pane.setStyle("-fx-background-image: url(/pic/Sign.png)");
        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.show();
    }

    public static String random() {
        String s = "";
        s += String.valueOf((int) (Math.random()*9)+1);
        for (int i = 2; i <= 6; i++) {
            s += String.valueOf((int) (Math.random()*10));
        }
        return s;
    }

    public static boolean idNumRight(String s) {
        if (s.length() != 18) {
            return false;
        }
        int sum;
        try{
        sum = Integer.parseInt(s.substring(0, 1)) * 7 +
                Integer.parseInt(s.substring(1, 2)) * 9 +
                Integer.parseInt(s.substring(2, 3)) * 10 +
                Integer.parseInt(s.substring(3, 4)) * 5 +
                Integer.parseInt(s.substring(4, 5)) * 8 +
                Integer.parseInt(s.substring(5, 6)) * 4 +
                Integer.parseInt(s.substring(6, 7)) * 2 +
                Integer.parseInt(s.substring(7, 8)) * 1 +
                Integer.parseInt(s.substring(8, 9)) * 6 +
                Integer.parseInt(s.substring(9, 10)) * 3 +
                Integer.parseInt(s.substring(10, 11)) * 7 +
                Integer.parseInt(s.substring(11, 12)) * 9 +
                Integer.parseInt(s.substring(12, 13)) * 10 +
                Integer.parseInt(s.substring(13, 14)) * 5 +
                Integer.parseInt(s.substring(14, 15)) * 8 +
                Integer.parseInt(s.substring(15, 16)) * 4 +
                Integer.parseInt(s.substring(16, 17)) * 2;}
        catch (NumberFormatException ex)
        {
            return false;
        }
        sum = sum % 11;
        char finalNum = s.charAt(17);
        switch (sum) {
            case 0:
                if (finalNum == '1') return true;
                else return false;
            case 1:
                if (finalNum == '0') return true;
                else return false;
            case 2:
                if (finalNum == 'X') return true;
                else return false;
            case 3:
                if (finalNum == '9') return true;
                else return false;
            case 4:
                if (finalNum == '8') return true;
                else return false;
            case 5:
                if (finalNum == '7') return true;
                else return false;
            case 6:
                if (finalNum == '6') return true;
                else return false;
            case 7:
                if (finalNum == '5') return true;
                else return false;
            case 8:
                if (finalNum == '4') return true;
                else return false;
            case 9:
                if (finalNum == '3') return true;
                else return false;
            case 10:
                if (finalNum == '2') return true;
                else return false;
        }
        return true;
    }

}
